package com.jing.mybroadcastreceiver;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.jing.mybroadcastreceiver.receiver.MyReceiver;

public class MainActivity extends AppCompatActivity {

 //   //                  动态广播
//    private MyReceiver myReceiver;
//    //                  动态广播

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        //                  静态广播
//        findViewById(R.id.btn).setOnClickListener(v -> {
//            Log.d("TAG", "onStart: -->");
//            Intent intent = new Intent("com.jing.mybroadcastreceiver.myReceiver");
//            intent.putExtra("msg", "Hello, world!");
//            sendBroadcast(intent);
//            Log.d("TAG", "onFinish: -->");
//        });
//        //                  静态广播




//        //                  动态广播
//        myReceiver = new MyReceiver();
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("com.jing.mybroadcastreceiver.myReceiver");
//        registerReceiver(myReceiver, intentFilter);
//        //                  动态广播



//        //                  有序广播
//        findViewById(R.id.btn).setOnClickListener(v -> {
//            Log.d("TAG", "onStart: -->");
//            Intent intent = new Intent("BROADCAST_ACTION");
//            intent.putExtra("msg", "hello word");
//            sendOrderedBroadcast(intent, null);
//            Log.d("TAG", "onFinish: -->");
//        });
//        //                  有序广播




        //                      应用内广播
        // 注册广播接收器
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("com.jing.ACTION_CUSTOM_BROADCAST"));

        // 发送广播
        findViewById(R.id.btn).setOnClickListener(v->{
            Intent intent = new Intent("com.jing.ACTION_CUSTOM_BROADCAST");
            intent.putExtra("message","hello , LocalBroadcastManager");
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        });
        //                      应用内广播

    }
    //                      应用内广播
    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 处理广播
            if ("com.jing.ACTION_CUSTOM_BROADCAST".equals(intent.getAction())){
                String message = intent.getStringExtra("message");
                Log.d("MainActivity", "Broadcast received!-->"+message);
            }
        }
    };
    //                      应用内广播


    

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }
    }
}
